﻿#region File Header
//-----------------------------------------------------------------------------
// Bricks' Bane
//
// Copyright (C) 2010 Julien Villers
// This program is distributed under the terms of the 
// GNU Lesser General Public License (LGPL).
// See Docs/lgpl.txt and Docs/gpl.txt
//-----------------------------------------------------------------------------
#endregion

using System;
using System.IO;
using BricksBane.Entities;
using Microsoft.Xna.Framework.Graphics;

namespace BricksBane.Building
{
    class BrickBuilder
    {
        private string[] m_Elements;
        private Color m_Color;

        public BrickBuilder()
        {
            m_Color = Color.Magenta;
            m_Elements = null;
        }

        public void Load(Wall wall, StreamReader reader)
        {
            bool isNextColor = false;
            Brick.E_Types nextBrickType = Brick.E_Types.Basic;

            while (!reader.EndOfStream)
            {
                string line = reader.ReadLine();
                m_Elements = line.Split(',');
                if (m_Elements.Length == 1)
                {
                    if (m_Elements[0] == "Color")
                    {
                        isNextColor = true;
                    }
                    else
                    {
                        nextBrickType = (Brick.E_Types)Enum.Parse(typeof(Brick.E_Types), m_Elements[0], false);
                    }
                    continue;
                }
                
                if (isNextColor && m_Elements.Length == 3)
                {
                    // New color
                    byte r, g, b;
                    r = byte.Parse(m_Elements[0]);
                    g = byte.Parse(m_Elements[1]);
                    b = byte.Parse(m_Elements[2]);
                    m_Color = new Color(r, g, b);
                    continue;
                }

                // New brick
                BuildBrick(wall, nextBrickType);

                isNextColor = false;
            }            
        }

        private void BuildBrick(Wall wall, Brick.E_Types brickType)
        {
            int idx = 0;
            int x = int.Parse(m_Elements[idx++]);
            int y = int.Parse(m_Elements[idx++]);
            int brickHP = 1;

            switch (brickType)
            {
                case Brick.E_Types.Basic:
                    break;
                case Brick.E_Types.Multiple:
                    brickHP = 3;// int.Parse(m_Elements[idx++]);
                    break;
                case Brick.E_Types.Unbreakable:
                    break;
                case Brick.E_Types.Nitro:
                    break;
            }

            Brick brick = wall.GetNewBrick();
            brick.Type = brickType;
            brick.HitPoints = brickHP;
            wall.SetupBrick(ref brick, x, y);
            brick.Color = m_Color;
        }
    }
}
